﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Protocol.stage;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Battle;
using Pixysoft.Weblications.Sango.Protocol.pve;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Weblications.Sango.Protocol
{
    class StageFacade
    {
        public static List<stage_rveteam> ToRveTeams(List<RveStageTeam> teams)
        {
            List<stage_rveteam> list = new List<stage_rveteam>();
            foreach (RveStageTeam team in teams)
            {
                list.Add(ToRveTeam(team));
            }
            return list;
        }

        public static stage_rveteam ToRveTeam(RveStageTeam team)
        {
            stage_rveteam body = ReflectionManager.CreatePojo<stage_rveteam>();
            body.roomcode = team.Roomcode;

            body.nickname = team.Creator.Nickname;
            body.usercode = team.Creator.Usercode;
            body.userlevel = team.Creator.Userlevel;
            body.avatar = team.Creator.Avatartypecode;

            body.stagename = team.Stage.Stagename;
            body.stagelevel = team.Stage.Stagelevel;
            body.membercount = team.Profiles.Length;
            body.totalmembercount = team.Stage.Stagemaxplayercount;
            body.battledate = team.Battledate;
            return body;
        }

        internal static stage_rvestage ToRveStage(Entities.War_Stage stage)
        {
            stage_rvestage body = ReflectionManager.CreatePojo<stage_rvestage>();
            body.stageserialcode = stage.Stageserialcode;
            body.stagename = stage.Stagename;
            body.stagelevel = stage.Stagelevel;
            body.playtimes = stage.Stageplaytimes;
            body.soul = stage.Soulbasevalue;
            if (stage.Equipmentserialcode != PlayerFormular.default_number_empty)
            {
                Entities.Eqm_Equipment eqm = EqmEquipmentHelper.searchBySerialcode(stage.Equipmentserialcode);
                body.eqm = eqm.Equipmentname;
            }
            else
            {
                body.eqm = "无";
            }
            body.defensecount = WarOfficerteamHelper.searchRveteams(stage.Stageserialcode).Count;
            body.minattack = stage.Stageminplayercount;
            body.maxattack = stage.Stagemaxplayercount;
            return body;
        }

        internal static stage_stage toStage(Entities.War_Stage stage, Entities.Usr_Profile profile)
        {
            stage_stage body = ReflectionManager.CreatePojo<stage_stage>();
            body.stageserialcode = stage.Stageserialcode;
            body.stagename = stage.Stagename;
            body.stagelevel = stage.Stagelevel;
            body.playtimes = stage.Stageplaytimes;
            if (body.playtimes <= 0)
                body.playtimes = 0;
            body.description = stage.Description;
            body.soul = StageFormular.GetStageSoulValue(stage, profile);
            if (stage.Equipmentserialcode != PlayerFormular.default_number_empty)
            {
                Entities.Eqm_Equipment eqm = EqmEquipmentHelper.searchBySerialcode(stage.Equipmentserialcode);
                body.eqm = eqm.Equipmentname;
            }
            else
            {
                body.eqm = "无";
            }
            body.ispass = false;
            body.isrve = (stage.Stagetypecode == WarStageTypecodeCollection.RVE);

            return body;
        }

        internal static stage_stage toStage(Pixysoft.Weblications.Sango.Entities.War_Stage stage, Pixysoft.Weblications.Sango.Entities.Usr_Stage usrstage)
        {
            stage_stage body = ReflectionManager.CreatePojo<stage_stage>();
            body.stageserialcode = stage.Stageserialcode;
            body.stagename = stage.Stagename;
            body.playtimes = stage.Stageplaytimes;
            body.description = stage.Description;
            body.soul = stage.Soulbasevalue;
            if (stage.Equipmentserialcode != PlayerFormular.default_number_empty)
            {
                Entities.Eqm_Equipment eqm = EqmEquipmentHelper.searchBySerialcode(stage.Equipmentserialcode);
                body.eqm = eqm.Equipmentname;
            }
            body.ispass = true;
            body.isrve = (stage.Stagetypecode == WarStageTypecodeCollection.RVE);

            return body;
        }

        internal static stage_map toMap(Pixysoft.Weblications.Sango.Entities.War_Map map)
        {
            stage_map body = ReflectionManager.CreatePojo<stage_map>();
            body.parentmapserialcode = map.Parentmapserialcode;
            body.mapserialcode = map.Mapserialcode;
            body.mapname = map.Mapname;
            body.description = map.Description;

            if (map.Equipmentserialcode != PlayerFormular.default_number_empty)
            {
                Entities.Eqm_Equipment eqm = EqmEquipmentHelper.searchBySerialcode(map.Equipmentserialcode);
                body.eqm = eqm.Equipmentname;
            }

            return body;
        }

        internal static List<stage_map> toMaps()
        {
            List<stage_map> list = new List<stage_map>();

            foreach (Entities.War_Map map in WarStageHelper.searchMaps())
            {
                list.Add(toMap(map));
            }

            return list;
        }

        public static List<stage_rvebattle> ToRveBattles(List<Entities.Usr_Rvebattle> orms)
        {
            List<stage_rvebattle> list = new List<stage_rvebattle>();

            foreach (Entities.Usr_Rvebattle orm in orms)
            {
                list.Add(ToRveBattle(orm));
            }

            return list;
        }

        public static stage_rvebattle ToRveBattle(Entities.Usr_Rvebattle orm)
        {
            Entities.War_Stage stage = WarStageHelper.searchStageByCode(orm.Stageserialcode);
            Entities.Sys_Section section = SysSectionHelper.searchSectionByCode(orm.Sectionserialcode);

            stage_rvebattle body = ReflectionManager.CreatePojo<stage_rvebattle>();
            body.code = orm.Primarycode;

            body.stagename = stage.Stagename;
            body.sectionname = section.Sectionname;
            body.soulvalue = orm.Soulvalue;
            if (orm.Equipmentserialcode != EquipmentFormular.default_equipment_none)
            {
                Entities.Eqm_Equipment eqm = EqmEquipmentHelper.searchBySerialcode(orm.Equipmentserialcode);
                body.equipmentname = eqm.Equipmentname;
            }
            else
            {
                body.equipmentname = "无";
            }
            body.fightdate = (long)orm.Fightdate;
            body.fightresult = SettingHelper.BooleanValue(orm.Fightresult);

            body.equipmentserialcode = orm.Equipmentserialcode;
            body.sectionserialcode = orm.Sectionserialcode;
            body.stageserialcode = orm.Stageserialcode;
            return body;
        }


        public static pve_war toVideo(string videocode, PvxBattleHistory history)
        {
            pve_war body = ReflectionManager.CreatePojo<pve_war>();
            body.code = videocode;
            body.result = history.BattleResult;
            body.attackers = toPveBattlers(history.Attacks);
            body.defensers = toPveBattlers(history.Defenses);
            body.actions = toPveActions(history, history.Actions);
            return body;
        }

        private static List<pve_action> toPveActions(PvxBattleHistory history, List<BattleActionHistory> orms)
        {
            List<pve_action> list = new List<pve_action>();

            int index = 0;
            foreach (BattleActionHistory action in orms)
            {
                list.Add(toPveAction(index++, history, action));
            }

            return list;
        }

        private static pve_action toPveAction(int index, PvxBattleHistory history, BattleActionHistory action)
        {
            pve_action body = ReflectionManager.CreatePojo<pve_action>();
            body.index = index;
            body.actioncode = action.ActionTypeCode;

            body.attack = action.Attacktypecode;
            body.magic = action.Attacktypecode;
            body.skill = action.Skilltypecode;

            body.attacker = toPveRunningAttacker(action);
            body.isattack = toPveActionIsAttacker(history, body.attacker.code);
            body.items = toPveActionItems(action.Items);
            return body;
        }

        private static bool toPveActionIsAttacker(PvxBattleHistory history, string battlercode)
        {
            foreach (BattlerHistory battler in history.Attacks.List)
            {
                if (battler.BattlerCode.Equals(battlercode, StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }

        private static pve_runningbattler toPveRunningAttacker(BattleActionHistory action)
        {
            pve_runningbattler body = ReflectionManager.CreatePojo<pve_runningbattler>();

            if (action.Items.Count == 0)
                throw Exceptions.VerificationFailedException(action);

            body.code = action.Attacker.BattlerCode;
            body.hp = action.Attacker.LeftHpPointValue;
            body.mp = action.Attacker.Powervalue;

            foreach (BattleActionItemHistory history in action.Items)
            {
                if (history.Attacker == null)
                    continue;

                if (!history.Attacker.BattlerCode.Equals(action.Attacker.BattlerCode, StringComparison.OrdinalIgnoreCase))
                    continue;

                body.hp = history.Attacker.LeftHpPointValue;
                body.mp = history.Attacker.Powervalue;
                //TODO status here
            }

            return body;
        }

        private static List<pve_actionitem> toPveActionItems(List<BattleActionItemHistory> orms)
        {
            List<pve_actionitem> list = new List<pve_actionitem>();

            int index = 0;
            foreach (BattleActionItemHistory history in orms)
            {
                pve_actionitem body = ReflectionManager.CreatePojo<pve_actionitem>();
                list.Add(body);

                body.index = index++;
                body.itemcode = history.ActionResult;
                body.defenser = toPveRunningBattler(history.Defenser);
            }

            return list;
        }

        private static pve_runningbattler toPveRunningBattler(BattlerHistory battlerHistory)
        {
            pve_runningbattler body = ReflectionManager.CreatePojo<pve_runningbattler>();
            body.code = battlerHistory.BattlerCode;
            body.hp = battlerHistory.LeftHpPointValue;
            body.mp = battlerHistory.Powervalue;
            return body;
        }

        private static List<pve_battler> toPveBattlers(BattleTeamHistory battleTeamHistory)
        {
            List<pve_battler> list = new List<pve_battler>();

            foreach (BattlerHistory history in battleTeamHistory.List)
            {
                pve_battler body = ReflectionManager.CreatePojo<pve_battler>();
                list.Add(body);

                body.index = history.PositionIndex;
                body.code = history.BattlerCode;
                body.name = history.BattlerName;
                body.hp = history.TotalHpPointValue;
                body.totalhp = history.TotalHpPointValue;
                body.mp = history.Powervalue;
            }

            return list;
        }
    }
}
